package com.tz.web;

import static com.tz.util.TzConstant.SESSION_ADM_USER;
import static com.tz.util.TzConstant.SESSION_ADM_USER_USERNAME;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tz.bean.Admin;
import com.tz.bean.TzParams;
import com.tz.core.BaseController;
import com.tz.service.user.IAdminService;
import com.tz.util.TmStringUtils;

@Controller
public class LoginController extends BaseController{

	@Autowired
	IAdminService adminService;
	
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	
	@ResponseBody
	@RequestMapping(value="/logined",method=RequestMethod.POST)
	public String logined(TzParams params){
		if(params!=null){
			if(TmStringUtils.isNotEmpty(params.getAccount())&&TmStringUtils.isNotEmpty(params.getPassword())){
				Admin  admin = adminService.getLogin(params);
				if(admin!=null){
					session.setAttribute(SESSION_ADM_USER, admin);
					session.setAttribute(SESSION_ADM_USER_USERNAME, admin.getUsername());
					return "success";
				}else {
					return "fail";
				}
			}else {
				return "null";
			}
		}else {
			return "error";
		}
	}
	
	@RequestMapping("/admlogout")
	public String admlogout(){
		session.invalidate();
		return "redirect:login";
	}
}
